<template>
  <div class="container">
    <div class="panda">
      <div class="ear left"></div>
      <div class="ear right"></div>
      <div class="face">
        <div class="eye-shadow left"></div>
        <div class="eye-white left" ref="leftEye">
          <div
            :style="{
              height: '20px',
              backgroundColor: '#000',
              borderRadius: '100%',
              position: 'absolute',
              left: leftEyeStyle.left,
              top: leftEyeStyle.top,
              width: '20px',
            }"
            ref="leftEyeBall"
          ></div>
        </div>

        <div class="eye-shadow right"></div>
        <div class="eye-white right" ref="rightEye">
          <div
            :style="{
              height: '20px',
              backgroundColor: '#000',
              borderRadius: '100%',
              position: 'absolute',
              left: rightEyeStyle.left,
              top: rightEyeStyle.top,
              width: '20px',
            }"
            ref="rightEyeBall"
          ></div>
        </div>
        <div class="nose"></div>
        <div class="mouth"></div>
      </div>
      <div class="body"></div>
      <div class="foot left">
        <div class="sole"></div>
      </div>
      <div class="foot right">
        <div class="sole"></div>
      </div>
    </div>
    <div class="bl-qrdialog" v-show="loginType == 1">
      <div class="hand left"></div>
      <div class="hand right"></div>
      <el-image :src="qrImg"> </el-image>
      <div style="display: flex">
        <el-button
          type="primary"
          :round="true"
          style="
            z-index: 5;
            background: linear-gradient(200deg, #37e2b2, #2fa080);
            width: 120px;
          "
          @click="()=>{loginType = 0}"
          ><span
            ><el-icon class="el-icon--right"
              ><ArrowLeft style="width: 20px; height: 10px" />
              <el-text class="mx-1">账号/密码登录</el-text></el-icon
            ></span
          ></el-button
        >
      </div>
    </div>
    <div
      class="login-box"
      :class="{ up: isPasswordFocused }"
      v-show="loginType == 0"
    >
      <div class="hand left"></div>
      <div class="hand right"></div>
      <h1>用户登录</h1>

      <div class="ipt-box">
        <input
          type="text"
          v-model="loginForm.username"
          @keydown.enter="login"
          required
        />
        <label>用户名</label>
      </div>
      <div class="ipt-box">
        <input
          type="password"
          id="password"
          v-model="loginForm.password"
          required
          @keydown.enter="login"
          @focus="isPasswordFocused = true"
          @blur="handleBlue"
        />
        <label>密码</label>
      </div>
      <p class="btn">
        <span class="login" @click="login">登录</span>
        <span>/</span>
        <router-link to="/register" class="register">注册</router-link>
      </p>
      <span class="otherLogin" style="margin-top: 10px">
        <hr style="width: 95px" />
        其他方式登陆
        <hr style="width: 95px" />
      </span>
      <div>
        <img
          src="@/assets/icons/wechat.svg"
          alt=""
          class="wechat"
          @click="wechatLogin"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, reactive, computed, nextTick } from "vue";
import { useRouter } from "vue-router";
import axios from "axios";

import {
  ArrowLeft,
  ArrowRight,
  Delete,
  Edit,
  Share,
} from "@element-plus/icons-vue";

const qrImg = ref();
const loginType = ref<number>(0);
const wechatLogin = () => {
  axios
    .post(
      "/api/plugin/getQrcode",
      {
        page: "pages/login/login",
        scene: "a=1",
        check_path: true,
        env_version: "release",
        is_hyaline: false,
        width: 260,
      },
      { responseType: "blob" }
    )
    .then((response) => {
      const reader = new FileReader();
      reader.readAsDataURL(response.data);
      reader.onloadend = function () {
        const base64data = reader.result;
        qrImg.value = base64data;
        let previewImg = new Image();

        previewImg.src = base64data as string;

        // 将图片的 Base64 编码设置为srcList的值

        loginType.value = 1;
      };
    });
};

let loginForm = reactive({
  username: "admin",
  password: "123456",
});

const router = useRouter();

const login = function () {
  if (!loginForm.username || !loginForm.password) {
    alert("请输入用户名和密码");
    return;
  }
  if (loginForm.username === "admin" && loginForm.password === "123456") {
    //登陆成功
    localStorage.setItem("token", "123456");
    router.push("/index");
  } else {
    loginForm = {
      username: "",
      password: "",
    };
    //登陆失败
    alert("用户名或密码错误");
  }
};
let isPasswordFocused = ref(false);
const handleBlue = () => {
  nextTick(() => {
    isPasswordFocused.value = false;
  });
};
const leftEye = ref();
const rightEye = ref();
const rightEyeBall = ref();
const leftEyeBall = ref();
const leftEyeStyle = computed(() => ({
  left: rightEyeBallPosition.left + "px",
  top: rightEyeBallPosition.top + "px",
}));
const rightEyeStyle = computed(() => ({
  left: rightEyeBallPosition.left + "px",
  top: rightEyeBallPosition.top + "px",
}));
const leftEyeBallPosition = reactive({ left: 5, top: 5 });
const rightEyeBallPosition = reactive({ left: 5, top: 5 });
// 计算眼球位置的函数
function calculateEyePosition(eyeElement: any, eyeBall: any, e: any) {
  //获取鼠标横坐标
  const mouseX = e.clientX;
  //获取鼠标纵坐标
  const mouseY = e.clientY;

  // 获取眼睛元素的位置和尺寸
  const eyeRect = eyeElement.getBoundingClientRect();
  // 计算眼睛中心点的坐标(坐标原点为视口左上角)
  const eyeCenterX = eyeRect.left + eyeRect.width / 2;
  const eyeCenterY = eyeRect.top + eyeRect.height / 2;

  //计算眼球的最大移动直径
  const maxRadius = eyeElement.clientWidth - eyeBall.clientWidth;

  //计算纵向比例尺
  const scaleY = window.innerHeight / maxRadius;

  const scaleX = window.innerWidth / maxRadius;

  //计算距离眼球中心横纵偏移量
  const offsetX = mouseX - eyeCenterX;
  const offsetY = mouseY - eyeCenterY;

  //计算眼球的新位置
  const newLeft = 5 + offsetX / scaleX;
  const newTop = 5 + offsetY / scaleY;

  return { left: newLeft, top: newTop };
}
const handleMouseMove = (e: MouseEvent) => {
  // 计算最大可移动半径

  // 计算左眼和右眼的位置
  const newRightEyeBallPosition = calculateEyePosition(
    rightEye.value,
    rightEyeBall.value,
    e
  );
  const newLeftEyeBallPosition = calculateEyePosition(
    leftEye.value,
    rightEyeBall.value,
    e
  );

  rightEyeBallPosition.left = newRightEyeBallPosition.left;
  rightEyeBallPosition.top = newRightEyeBallPosition.top;
  leftEyeBallPosition.left = newLeftEyeBallPosition.left;
  leftEyeBallPosition.top = newLeftEyeBallPosition.top;
};

onMounted(() => {
  document.addEventListener("mousemove", handleMouseMove);
});

onUnmounted(() => {
  document.removeEventListener("mousemove", handleMouseMove);
});
</script>
<style scoped lang="scss">
* {
  /* 初始化 */
  margin: 0;
  padding: 0;
}
:deep() {
  @include b(qrdialog) {
    width: 400px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -110px);
    z-index: 2;
    /* 弹性布局 居中 垂直排列 */
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    /* 设置过渡 */
    transition: 0.3s;
    z-index: 3;
  }
  .el-dialog__header {
    height: 0px;
  }
  .el-image__inner {
    width: 275px;
    height: 275px;
  }
}

.container {
  /* 100%窗口高度 */
  height: 100vh;
  /* 弹性布局 水平+垂直居中 */
  display: flex;
  justify-content: center;
  align-items: center;
  /* 渐变背景 */
  background: linear-gradient(200deg, #37e2b2, #2fa080);
}
/* 开始画熊猫 */
.panda {
  /* 相对定位 */
  position: relative;
  width: 200px;
}
/* 脸部 */
.face {
  width: 200px;
  height: 200px;
  background-color: #fff;
  border-radius: 100%;
  box-shadow: 0 10px 15px rgba(0, 0, 0, 0.15);
  position: relative;
  z-index: 1;
}
/* 耳朵 */
.ear {
  width: 70px;
  height: 70px;
  background-color: #000;
  border-radius: 100%;
  position: absolute;
  top: -10px;
}
.ear.right {
  right: 0;
}
/* 黑眼圈 */
.eye-shadow {
  width: 50px;
  height: 80px;
  background-color: #000;
  border-radius: 50%;
  /* 绝对定位 */
  position: absolute;
  top: 40px;
}
.eye-shadow.left {
  transform: rotate(45deg);
  left: 30px;
}
.eye-shadow.right {
  transform: rotate(-45deg);
  right: 30px;
}
/* 眼白 */
.eye-white {
  width: 30px;
  height: 30px;
  background-color: #fff;
  border-radius: 100%;
  position: absolute;
  top: 68px;
}
.eye-white.left {
  left: 38px;
}
.eye-white.right {
  right: 38px;
}
/* 眼球 */
.eye-ball {
  width: 20px;
  height: 20px;
  background-color: #000;
  border-radius: 100%;
  position: absolute;
  left: 5px;
  top: 5px;
}
/* 鼻子 */
.nose {
  width: 35px;
  height: 20px;
  background-color: #000;
  position: absolute;
  left: 0;
  right: 0;
  margin: auto;
  bottom: 65px;
  border-radius: 42px 42px 60px 60px / 30px 30px 60px 60px;
}
/* 嘴巴 */
.mouth {
  width: 68px;
  height: 25px;
  border-bottom: 7px solid #000;
  border-radius: 50%;
  position: absolute;
  left: 0;
  right: 0;
  margin: auto;
  bottom: 35px;
}
/* 身体 */
.body {
  width: 250px;
  height: 280px;
  background-color: #fff;
  position: relative;
  left: -25px;
  top: -10px;
  border-radius: 100px 100px 100px 100px / 126px 126px 96px 96px;
  box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
}
/* 脚 */
.foot {
  width: 82px;
  height: 120px;
  background-color: #000;
  position: absolute;
  bottom: 8px;
  z-index: 3;
  border-radius: 40px 40px 35px 40px / 26px 26px 63px 63px;
  box-shadow: 0 5px 5px rgba(0, 0, 0, 0.2);
}
.foot.left {
  left: -80px;
}
.foot.right {
  right: -80px;
  transform: rotateY(180deg);
}
/* 脚掌-大椭圆 */
.foot::after {
  content: "";
  width: 55px;
  height: 65px;
  background-color: #222;
  position: absolute;
  border-radius: 50%;
  left: 0;
  right: 0;
  margin: auto;
  bottom: 10px;
}
/* 脚掌-三个小椭圆 */
.foot .sole,
.foot .sole::before,
.foot .sole::after {
  width: 20px;
  height: 30px;
  background-color: #222;
  position: absolute;
  border-radius: 50%;
  left: 0;
  right: 0;
  margin: auto;
  top: 8px;
}
.foot .sole::before {
  content: "";
  left: -50px;
}
.foot .sole::after {
  content: "";
  left: 25px;
}
/* 手 */
.hand,
.hand::before,
.hand::after {
  width: 40px;
  height: 30px;
  background-color: #000;
  border-radius: 50px;
  position: absolute;
  top: 70px;
  left: -20px;
}
.hand::before {
  content: "";
  top: 16px;
  left: 0;
}
.hand::after {
  content: "";
  top: 32px;
  left: 0;
}
.hand.right {
  right: -20px;
  left: auto;
}
/* 登录框 */
.login-box {
  width: 400px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -110px);
  z-index: 2;
  /* 弹性布局 居中 垂直排列 */
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  /* 设置过渡 */
  transition: 0.3s;
}
h1 {
  color: #1dc797;
  font-weight: normal;
  margin-bottom: 5px;
}
.ipt-box {
  width: 80%;
  margin-top: 25px;
  position: relative;
}
.ipt-box input {
  width: 100%;
  height: 35px;
  border: none;
  border-bottom: 1px solid #bbb;
  text-indent: 5px;
  outline: none;
  transition: 0.3s;
}
.ipt-box label {
  position: absolute;
  left: 5px;
  top: 5px;
  font-size: 14px;
  color: #888;
  transition: 0.3s;
  pointer-events: none;
}
/* 输入框选中或有值时输入框的样式 */
.ipt-box input:focus,
.ipt-box input:valid {
  border-color: #1dc797;
  box-shadow: 0 1px #1dc797;
}
/* 输入框选中或有值时label的样式 */
.ipt-box input:focus ~ label,
.ipt-box input:valid ~ label {
  color: #1dc797;
  font-size: 12px;
  transform: translateY(-15px);
}
.btn {
  width: 150px;
  height: 40px;
  display: flex;
  justify-content: space-between;
  line-height: 40px;
  background-color: #1dc797;
  border: none;
  border-radius: 3px;
  padding: 0 10px;
  margin-top: 30px;
  color: #fff;
  letter-spacing: 8px;
  text-indent: 10px;
  cursor: pointer;
  transition: 0.3s;

  & .login {
    margin-left: -10px;
    width: 40%;
    line-height: 40px;
  }
  & .register {
    color: #fff;
    text-decoration: none;
    margin-right: -10px;
    width: 40%;
    line-height: 40px;
  }
}
.login:hover {
  background-color: #6cd4b7;
}
.register:hover {
  background-color: #6cd4b7;
}
/* 登录框向上举 */
.up {
  transform: translate(-50%, -180px);
}
</style>
